package com.ymatou.iapi.couponmanage.old.testcase;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponmanage.old.parameter.BatchInfo;
import com.ymatou.iapi.couponmanage.old.parameter.BatchList;
import com.ymatou.iapi.couponmanage.old.parameter.QueryValidBatchListBean;
import com.ymatou.iapi.couponmanage.old.parameter.SendCouponBean;
import com.ymatou.iapi.couponmanage.old.service.QueryValidBatchListCall;
import com.ymatou.iapi.couponmanage.old.service.SendCouponCall;
import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymatou.promotion.api.service.CreateSellerCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.CouponManageCallService;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.EnvSetup;
/**
*商家查询有效的优惠券列表用例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryValidBatchList{
	private static QueryValidBatchListBean queryvalidbatchlistBean;
	private static QueryValidBatchListCall queryvalidbatchlistCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家查询有效的优惠券列表用例");
	}

	@Before
	public void caseUp() {
		queryvalidbatchlistBean=new QueryValidBatchListBean();
		queryvalidbatchlistCall=new QueryValidBatchListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryvalidbatchlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_001() {
		Logger.start(true,"商家查询有效的优惠券列表");
		try {
			setQueryValidBatchListBean();
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			VerifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_002() {
		Logger.start(true,"反例-商家查询有效的优惠券列表-setPageIndex=0,报错");
		try {
			setQueryValidBatchListBean();
			queryvalidbatchlistBean.setPageIndex(0);
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 Logger.verifyEquals("0", queryvalidbatchlistCall.getCode(),
		                "验证返回code");
		     
			 Logger.verifyEquals("操作成功！",
		        		queryvalidbatchlistCall.getMessage(), "验证返回Message");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_003() {
		Logger.start(true,"商家查询有效的优惠券列表-PageSize=0,报错");
		try {
			setQueryValidBatchListBean();
			queryvalidbatchlistBean.setPageSize(0);
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 Logger.verifyEquals("3", queryvalidbatchlistCall.getCode(),
		                "验证返回code");
		     
			 Logger.verifyEquals("指定的转换无效。",
		        		queryvalidbatchlistCall.getMessage(), "验证返回Message");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_004() {
		Logger.start(true,"商家查询有效的优惠券列表-PageSize=1,返回一条数据");
		try {
			setQueryValidBatchListBean();
			queryvalidbatchlistBean.setPageSize(1);
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 Logger.verifyEquals("0", queryvalidbatchlistCall.getCode(),
		                "验证返回code");
		     
			 Logger.verifyEquals("操作成功！",
		        		queryvalidbatchlistCall.getMessage(), "验证返回Message");
			 
			 List<BatchInfo> list=queryvalidbatchlistCall.getBatchList().getBatchList();
			 Logger.verifyEquals(1,
					 list.size(), "验证返回条数");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_005() {
		Logger.start(true,"商家查询有效的优惠券列表-BatchCode非空");
		try {
			
			CouponManageCallService couponManageCallService = new CouponManageCallService();
			SendCouponBean sendcouponBean = couponManageCallService .CreateDefaultSendCouponBean();
			SendCouponCall sendCouponCall = couponManageCallService.SendCoupon(sendcouponBean);
			
			setQueryValidBatchListBean();
			queryvalidbatchlistBean.setBatchCode(sendcouponBean.getTaskSendReqInfo().getBatchCode());
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 VerifyReturn();
			 
			 Logger.verifyEquals(1,
		        		queryvalidbatchlistCall.getBatchList().getBatchList().size(), "验证返回条数");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_006() {
		Logger.start(true,"商家查询有效的优惠券列表-BatchName非空");
		try {
			
			CouponCallService couponCallService = new CouponCallService();
			
			CouponBatchBean createCouponBean = couponCallService.setDefaultCouponBatchBean();
			CreateSellerCouponCall  createCouponCall = couponCallService.createSellerCoupon(createCouponBean);
			
			CouponManageCallService couponManageCallService = new CouponManageCallService();
			SendCouponBean sendcouponBean = couponManageCallService .CreateDefaultSendCouponBean(createCouponCall.getBatchCode());
			SendCouponCall sendCouponCall = couponManageCallService.SendCoupon(sendcouponBean);
			
			setQueryValidBatchListBean();
			queryvalidbatchlistBean.setBatchName(createCouponBean.getCouponName());;
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 VerifyReturn();
			 
			 Logger.verifyEquals(1,
		        		queryvalidbatchlistCall.getBatchList().getBatchList().size(), "验证返回条数");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_007() {
		Logger.start(true,"商家查询有效的优惠券列表-BatchCode非空,BatchName非空");
		try {
			
			CouponCallService couponCallService = new CouponCallService();
			
			CouponBatchBean createCouponBean = couponCallService.setDefaultCouponBatchBean();
			CreateSellerCouponCall  createCouponCall = couponCallService.createSellerCoupon(createCouponBean);
			
			CouponManageCallService couponManageCallService = new CouponManageCallService();
			SendCouponBean sendcouponBean = couponManageCallService .CreateDefaultSendCouponBean(createCouponCall.getBatchCode());
			SendCouponCall sendCouponCall = couponManageCallService.SendCoupon(sendcouponBean);
			
			setQueryValidBatchListBean();

			queryvalidbatchlistBean.setBatchCode(createCouponCall.getBatchCode());
			queryvalidbatchlistBean.setBatchName(createCouponBean.getCouponName());;
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 VerifyReturn();
			 
			 Logger.verifyEquals(1,
		        		queryvalidbatchlistCall.getBatchList().getBatchList().size(), "验证返回条数");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_008() {
		Logger.start(true,"商家查询有效的优惠券列表-BatchCode非空,BatchName非空,BatchCode与BatchName不匹配");
		try {
			
			CouponCallService couponCallService = new CouponCallService();
			
			CouponBatchBean createCouponBean = couponCallService.setDefaultCouponBatchBean();
			CreateSellerCouponCall  createCouponCall = couponCallService.createSellerCoupon(createCouponBean);
			
			CouponManageCallService couponManageCallService = new CouponManageCallService();
			SendCouponBean sendcouponBean = couponManageCallService .CreateDefaultSendCouponBean(createCouponCall.getBatchCode());
			SendCouponCall sendCouponCall = couponManageCallService.SendCoupon(sendcouponBean);
			
			setQueryValidBatchListBean();

			queryvalidbatchlistBean.setBatchCode(createCouponCall.getBatchCode());
			queryvalidbatchlistBean.setBatchName(createCouponBean.getCouponName()+"233333");;
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 Logger.verifyEquals("0", queryvalidbatchlistCall.getCode(),
		                "验证返回code");
		     
		     Logger.verifyEquals("操作成功！",
		        		queryvalidbatchlistCall.getMessage(), "验证返回Message");
		        
			 Logger.verifyEquals(0,
		        		queryvalidbatchlistCall.getBatchList().getBatchList().size(), "验证返回条数");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void setQueryValidBatchListBean()
	{
		queryvalidbatchlistBean.setSellerId(Integer.valueOf(EnvSetup.getData("SellerId")));
		queryvalidbatchlistBean.setPageIndex(1);
		queryvalidbatchlistBean.setPageSize(100);
	}
	
	private void VerifyReturn() {
        Logger.verifyEquals("0", queryvalidbatchlistCall.getCode(),
                "验证返回code");
     
        Logger.verifyEquals("操作成功！",
        		queryvalidbatchlistCall.getMessage(), "验证返回Message");
        
        List<BatchInfo> batchList = queryvalidbatchlistCall.getBatchList().getBatchList();
        
        for(int i=0;i<batchList.size();i++)
        {
        	 String batchCode = batchList.get(i).getBatchCode();
        	 BatchInfo batchInfo = batchList.get(i);
        	 
        	 Map item = new YmtCouponBatchWapper().selectBatchInfoBysBatchCode(batchCode).get(0);

             Logger.verifyEquals(Integer.parseInt(item.get("iBatchId").toString()),
             		batchInfo.getBatchId(),
                     "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                             + "记录中iBatchId字段相同");
             
             Logger.verifyEquals(item.get("sBatchCode").toString(),
             		batchInfo.getBatchCode(),
                     "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                             + "记录中sBatchCode字段相同");

             // 是否作废
             if(item.get("bInvalidStatus")==null)
             {
             	Logger.verifyEquals(false,
                 		batchInfo.getIsCancellation(),
                         "验证返回IsCancellation与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                                 + "记录中bInvalidStatus字段相同");
             }
             else {
             	Logger.verifyEquals(Boolean.parseBoolean(item.get("bInvalidStatus").toString()),
                 		batchInfo.getIsCancellation(),
                         "验证返回IsCancellation与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                                 + "记录中bInvalidStatus字段相同");
     		}
             

          // 是否过期
             if(item.get("iEffectiveType")!=null && Integer.parseInt(item.get("iEffectiveType").toString())==1)
             {
             	Date date =new Date(item.get("dValidEnd").toString());
             	
             	Logger.verifyEquals(date.before(new Date()),
             		batchInfo.getIsExpire(),
                     "验证返回IsExpire正确");
             }
             
          // 是否领完
             Logger.verifyEquals(Integer.parseInt(item.get("iCouponTotalNum").toString())<Integer.parseInt(item.get("iReceiveCount").toString()),
             		batchInfo.getIsReceiveOver(),
                     "验证返回IsReceiveOver正确");
             
        
             Logger.verifyEquals(Integer.parseInt(item.get("iSendType").toString()),
             		batchInfo.getSendType(),
                     "验证返回SendType与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                             + "记录中iSendType字段相同");
             
             if(item.get("iEffectiveType")==null)
             {
             	Logger.verifyEquals(0,
             		batchInfo.getEffectiveType(),
                     "验证返回EffectiveType与数据库中表Ymt_CouponSetting中iEffectiveType字段相同");
             }
             else{
             	   Logger.verifyEquals(Integer.parseInt(item.get("iEffectiveType").toString()),
                    		batchInfo.getEffectiveType(),
                            "验证返回EffectiveType与数据库中表Ymt_CouponSetting中iEffectiveType字段相同");
             }
             
             if(item.get("iEffectiveType")==null)
             {
             	 Logger.verifyEquals(0,
                  		batchInfo.getEffectiveValidType(),
                          "验证返回EffectiveValidType与数据库中表Ymt_CouponSetting中iEffectiveValidType字段相同");
                  }
             else
             {
             	 Logger.verifyEquals(Integer.parseInt(item.get("iEffectiveValidType").toString()),
                  		batchInfo.getEffectiveValidType(),
                          "验证返回EffectiveValidType与数据库中表Ymt_CouponSetting中iEffectiveValidType字段相同");
             
             }

             Logger.verifyEquals(item.get("dValidStart").toString().subSequence(0, 19),
             		batchInfo.getValidStart().replace("T", " "),
                     "验证返回dValidStart与数据库中表Ymt_CouponSetting中dValidStart字段相同");

             Logger.verifyEquals(item.get("dValidEnd").toString().subSequence(0, 19),
             		batchInfo.getValidEnd().replace("T", " "),
                     "验证返回dValidEnd与数据库中表Ymt_CouponSetting中dValidEnd字段相同");
             
             if(item.get("iEffectiveDays")==null)
             {
             	Logger.verifyEquals(0,
             		batchInfo.getEffectiveDays(),
                     "验证返回iEffectiveDays与数据库中表Ymt_CouponSetting中iEffectiveDays字段相同");
             }
             else {
             	Logger.verifyEquals(Integer.parseInt(item.get("iEffectiveDays").toString()),
                 		batchInfo.getEffectiveDays(),
                         "验证返回iEffectiveDays与数据库中表Ymt_CouponSetting中iEffectiveDays字段相同");     
     		}
             
             Logger.verifyEquals(Integer.parseInt(item.get("iCouponTotalNum").toString()),
             		batchInfo.getTotalCount(),
                     "验证返回TotalCount与数据库中表Ymt_CouponBatch中iCouponTotalNum字段相同");
             
             Logger.verifyEquals(Integer.parseInt(item.get("iReceiveCount").toString()),
             		batchInfo.getReceivedCount(),
                     "验证返回BatchCode与数据库中表Ymt_CouponSetting中iReceiveCount字段相同");
             
             if(item.get("IsShowInPage")==null)
             {
             	Logger.verifyEquals(false,
             		batchInfo.getIsShowInPage(),
                     "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                             + "记录中IsShowInPage字段相同");
             }
             else {
                 Logger.verifyEquals(Boolean.parseBoolean(item.get("IsShowInPage").toString()),
                 		batchInfo.getIsShowInPage(),
                         "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                                 + "记录中IsShowInPage字段相同");
     		}
             
             Logger.verifyEquals(Integer.parseInt(item.get("iUserType").toString()),
             		batchInfo.getUserType(),
                     "验证返回BatchCode与数据库中表Ymt_CouponScenario中iUserType字段相同");
             
             if(item.get("sSellerIds")==null)
             {
             	Logger.verifyEquals(null,
             		batchInfo.getSellerIds(),
                     "验证返回BatchCode与数据库中表Ymt_CouponScenario中sSellerIds字段相同");
             }
             else {
             	Logger.verifyEquals(item.get("sSellerIds").toString(),
                 		batchInfo.getSellerIds(),
                         "验证返回BatchCode与数据库中表Ymt_CouponScenario中sSellerIds字段相同");
     		}
             
             if(item.get("sProductCategories")==null)
             {
             	Logger.verifyEquals(null,
             		batchInfo.getProductCategories(),
                     "验证返回BatchCode与数据库中表Ymt_CouponScenario中sProductCategories字段相同");
             }
             else {
             	Logger.verifyEquals(item.get("sProductCategories").toString(),
                 		batchInfo.getProductCategories(),
                         "验证返回BatchCode与数据库中表Ymt_CouponScenario中sProductCategories字段相同");
     		}
             
             if(item.get("sSpecificProducts")==null)
             {
             	Logger.verifyEquals(null,
             		batchInfo.getSpecificProducts(),
                     "验证返回BatchCode与数据库中表Ymt_CouponScenario中sSpecificProducts字段相同");
             }
             else {
             	Logger.verifyEquals(item.get("sSpecificProducts").toString(),
                 		batchInfo.getSpecificProducts(),
                         "验证返回BatchCode与数据库中表Ymt_CouponScenario中sSpecificProducts字段相同");
     		}
             
             if(item.get("sProductBrands")==null)
             {
             	Logger.verifyEquals(null,
             		batchInfo.getProductBrands(),
                     "验证返回BatchCode与数据库中表Ymt_CouponScenario中sProductBrands字段相同");
             }
             else {
             	 Logger.verifyEquals(item.get("sProductBrands").toString(),
                  		batchInfo.getProductBrands(),
                          "验证返回BatchCode与数据库中表Ymt_CouponScenario中sProductBrands字段相同");
               
     		}
             
             if(item.get("sActivityIds")==null)
             {
             	Logger.verifyEquals(null,
             		batchInfo.getActivityIds(),
                     "验证返回BatchCode与数据库中表Ymt_CouponScenario中sActivityIds字段相同");
             }
             else {
             	  Logger.verifyEquals(item.get("sActivityIds").toString(),
                   		batchInfo.getActivityIds(),
                           "验证返回BatchCode与数据库中表Ymt_CouponScenario中sActivityIds字段相同");        
     		}
             
             if(item.get("sUsePlatforms")==null)
             {
             	Logger.verifyEquals(null,
             		batchInfo.getUsePlatforms(),
                     "验证返回BatchCode与数据库中表Ymt_CouponScenario中sUsePlatforms字段相同");
             }
             else {
             	Logger.verifyEquals(item.get("sUsePlatforms").toString(),
                 		batchInfo.getUsePlatforms(),
                         "验证返回BatchCode与数据库中表Ymt_CouponScenario中sUsePlatforms字段相同");
     		}

        }

       
    }
}